package com.example.swd1.gudong.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.swd1.gudong.R;
import com.example.swd1.gudong.constant.Constant;


//==================================================================================================================
//   Author :  Shile Wu
//   Date  :  16-8-23
//   Role :    item in TabView
//   Reference documents : None
//==================================================================================================================
public class TabItem extends LinearLayout {

    private Context mContext = null;
    private ImageView mImageView = null;
    private TextView mTextView = null;
    private ImageView unReadRedPoint = null;
    private TextView unReadNum =null;
    private final static int DEFAULT_IMAGE_WIDTH = 50;
    private final static int DEFAULT_IMAGE_HEIGHT = 50;
    private int CHECKED_COLOR = getResources().getColor(R.color.codoon_green_text);
    private int UNCHECKED_COLOR = Color.GRAY;

    public TabItem(Context context) {
        super(context);
        mContext = context;
    }

    public TabItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View parentView = inflater.inflate(R.layout.tab_item, this, true);
        mImageView = (ImageView) parentView.findViewById(R.id.image_iamge_text);
        mTextView = (TextView) parentView.findViewById(R.id.text_iamge_text);
        unReadRedPoint = (ImageView) parentView.findViewById(R.id.unread_point);
        unReadNum = (TextView) parentView.findViewById(R.id.unread_num);
    }

    public void setImage(int id) {
        if (mImageView != null) {
            mImageView.setImageResource(id);
//            setImageSize(DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_HEIGHT);
        }
    }

    public void setText(String s) {
        if (mTextView != null) {
            mTextView.setText(s);
            mTextView.setTextColor(UNCHECKED_COLOR);
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
        return true;
    }

    private void setImageSize(int w, int h) {
        if (mImageView != null) {
            ViewGroup.LayoutParams params = mImageView.getLayoutParams();
            params.width = w;
            params.height = h;
            mImageView.setLayoutParams(params);
        }
    }

    public void setChecked(int itemID) {
        if (mTextView != null) {
            mTextView.setTextColor(CHECKED_COLOR);
        }
        int checkDrawableId = -1;
        switch (itemID) {
            case Constant.BTN_FLAG_SPORTRANG:
                checkDrawableId = R.drawable.ic_tab_sport_rang_selected;
                break;
            case Constant.BTN_FLAG_MESSAGE:
                checkDrawableId = R.drawable.ic_tab_msg_selected;
                break;
            case Constant.BTN_FLAG_ME:
                checkDrawableId = R.drawable.ic_tab_me_selected;
                break;
            case Constant.BTN_FLAG_FOUND:
                checkDrawableId = R.drawable.ic_tab_dis_selected;
                break;
            case Constant.BTN_FLAG_SPORT:
                checkDrawableId = R.drawable.ic_tab_sport_selected;
                break;
            default:
                break;
        }
        if (mImageView != null) {
            mImageView.setImageResource(checkDrawableId);
        }
    }

    public void setUnReadMessage(int num){
        unReadRedPoint.setVisibility(VISIBLE);
        unReadNum.setVisibility(VISIBLE);
        unReadNum.setText(num+"");
    }

    public void hideUnReadMessage(){
        unReadRedPoint.setVisibility(GONE);
        unReadNum.setVisibility(GONE);
        unReadNum.setText("0");
    }

    public String getUnReadMessage(){
        return unReadNum.getText().toString();
    }

}
